package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.dto.GoodsAllinone;
import org.linlinjava.litemall.admin.service.AdminArrivalReminderService;
import org.linlinjava.litemall.admin.service.AdminGoodsService;
import org.linlinjava.litemall.core.core.core.controller.BaseController;
import org.linlinjava.litemall.core.util.ExcelUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.dao.LitemallSubscriptionMapper;
import org.linlinjava.litemall.db.domain.LitemallArrivalReminder;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallGoodsProduct;
import org.linlinjava.litemall.db.domain.vo.LitemallGoodsDto;
import org.linlinjava.litemall.db.service.LitemallArrivalReminderService;
import org.linlinjava.litemall.db.service.LitemallGoodsProductService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/goods")
@Validated
public class AdminGoodsController extends BaseController {
    private final Log logger = LogFactory.getLog(AdminGoodsController.class);

    @Autowired
    private AdminGoodsService adminGoodsService;
    @Autowired
    private LitemallGoodsService goodsService;
    @Resource
    private LitemallSubscriptionMapper litemallSubscriptionMapper;
    @Autowired
    private LitemallGoodsProductService litemallGoodsProductService;


    /**
     * 查询最新商品前十
     *
     * @return
     */
    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"商品查询", "商品查询"}, button = "查询前10")
    @GetMapping("/listGoods")
    @ResponseBody
    public Object listGoods(String name) {
        return goodsService.queryByName(1, 10, name);

    }

    /**
     * 查询可借的书的列表
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/listCanBorrowed")
    @ResponseBody
    public Object listCanBorrowed(String name,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer limit,
                                  @RequestParam(required = false, defaultValue = "add_time") String sort,
                                  @RequestParam(required = false, defaultValue = "desc") String order) {
        return ResponseUtil.ok(goodsService.queryByCanBorrowAndName(name, page - 1, limit, sort, order));
    }

    /**
     * 查询商品
     *
     * @param goodsId
     * @param goodsSn
     * @param name
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "查询")
    @GetMapping("/list")
    @ResponseBody
    public Object list(Integer goodsId, String goodsSn, String name, String author,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        startPage();
        List<LitemallGoods> list = adminGoodsService.list(goodsId, goodsSn, name, author, page, limit, sort, order);
        return ResponseUtil.okList(list);
    }

    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"导出商品列表", "导出商品列表"}, button = "查询")
    @PostMapping("/exportList")
    public Object exportList(Integer goodsId, String goodsSn, String name, String author,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             @Sort @RequestParam(defaultValue = "add_time") String sort,
                             @Order @RequestParam(defaultValue = "desc") String order,
                             HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<LitemallGoods> list = adminGoodsService.list(goodsId, goodsSn, name, author, 1, 100000, sort, order);
        return ResponseUtil.ok(new ExcelUtil().exportExcel(request, response, list, "member"));
    }

    @GetMapping("/catAndBrand")
    @ResponseBody
    public Object list2() {
        return adminGoodsService.list2();
    }

    /**
     * 编辑商品
     *
     * @param dto
     * @return
     */
    @RequiresPermissions("admin:goods:update")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "编辑")
    @PostMapping("/update")
    @ResponseBody
    public Object update(@RequestBody LitemallGoodsDto dto) {
        return adminGoodsService.update(dto);
    }

    /**
     * 更新是否热卖
     *
     * @param goodsId
     * @return
     */
    @PostMapping("/updateIsHot")
    @ResponseBody
    public Object updateIsHot(@RequestParam Integer goodsId) {
        return adminGoodsService.updateIsHot(goodsId);
    }

    /**
     * 删除商品
     *
     * @param goods
     * @return
     */
    @RequiresPermissions("admin:goods:delete")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "删除")
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody LitemallGoods goods) {
        return adminGoodsService.delete(goods);
    }

    /**
     * 添加商品
     *
     * @param dto
     * @return
     */
    @RequiresPermissions("admin:goods:create")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "上架")
    @PostMapping(value = "/create", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object create(@RequestBody @Valid LitemallGoodsDto dto) {

        return adminGoodsService.create(dto);
    }

    /**
     * 商品详情
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:goods:read")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "详情")
    @GetMapping("/detail")
    @ResponseBody
    public Object detail(@NotNull Integer id) {
        return adminGoodsService.detail(id);
    }

    /**
     * 商品批量查询详情
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("admin:goods:reads")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品管理"}, button = "批量详情")
    @PostMapping("/details")
    @ResponseBody
    public Object details(@RequestBody Integer[] ids) {
        if (ids.length == 0) {
            return new ArrayList<>();
        }
        return ResponseUtil.ok(goodsService.queryByIds(ids));
    }

    /**
     * 通过isbn码查询商品
     *
     * @param isbn13
     * @return
     */
    @GetMapping("/queryByISBN13")
    @ResponseBody
    public Object queryByISBN13(@RequestParam(value = "isbn13") String isbn13) {
        return ResponseUtil.ok(adminGoodsService.queryByISBN13(isbn13));
    }

}
